package com.foto;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.camarasmactel.R;
import com.conexion.Utilitario;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Foto extends Activity implements OnClickListener{

	ImageView img;
	Button otra,guardar,volver;
	TextView hora,fecha;
	Calendar d;
	ProgressDialog pd;
	Utilitario util;
	Bitmap imagen;
	String fechahoy;
	String dia,mes,anho; int horas,minutos,segundos;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		pd = ProgressDialog.show(this, "Conectando...", "Conectando...");
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.imagenfoto);
		
		Calendar c;
		c=Calendar.getInstance();
		dia = Integer.toString(c.get(Calendar.DATE));
        mes = Integer.toString((c.get(Calendar.MONTH))+1);
        anho = Integer.toString(c.get(Calendar.YEAR));
        horas = c.get(Calendar.HOUR_OF_DAY);
        minutos = c.get(Calendar.MINUTE);
        segundos = c.get(Calendar.SECOND);
		
		util = new Utilitario();
		d = Calendar.getInstance();
		hora = (TextView) findViewById(R.id.hora);
		hora.setText(d.getTime().toString());
		fechahoy=d.getTime().toString();
		SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        String formattedDate = df.format(d.getTime());
		fecha = (TextView) findViewById(R.id.fecha);
		fecha.setText(formattedDate);
		
		img = (ImageView) findViewById(R.id.imagefoto);
		
		guardar = (Button) findViewById(R.id.guardar);
		guardar.setOnClickListener(this);
		volver = (Button) findViewById(R.id.volver);
		volver.setOnClickListener(this);
		otra = (Button) findViewById(R.id.otrafoto);
		otra.setOnClickListener(this);
		
		ObtenerFoto obtener = new ObtenerFoto();
		obtener.start();
		
		try {
			obtener.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		img.setImageBitmap(obtener.bmImg);
		imagen = obtener.bmImg;
		pd.dismiss();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		if (v.getId()==otra.findViewById(R.id.otrafoto).getId()){
			
			Calendar c;
			c=Calendar.getInstance();
			dia = Integer.toString(c.get(Calendar.DATE));
	        mes = Integer.toString((c.get(Calendar.MONTH))+1);
	        anho = Integer.toString(c.get(Calendar.YEAR));
	        horas = c.get(Calendar.HOUR_OF_DAY);
	        minutos = c.get(Calendar.MINUTE);
	        segundos = c.get(Calendar.SECOND);
			
			pd = ProgressDialog.show(this, "Conectando...", "Conectando...");	
			
			ObtenerFoto obtener = new ObtenerFoto();
			obtener.start();
			try {
				obtener.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			img.setImageBitmap(obtener.bmImg);
			imagen = obtener.bmImg;
			
			d = Calendar.getInstance();
			hora.setText(d.getTime().toString());
			SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
	        String formattedDate = df.format(d.getTime());
			fecha.setText(formattedDate);
		
			pd.dismiss();
		}
		if (v.getId()==guardar.findViewById(R.id.guardar).getId()){

			guardar();
		}
		if (v.getId()==volver.findViewById(R.id.volver).getId()){
			
				this.finish();
		}
	}
	
	private void guardar(){
		
		String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                "/CamaraFoto "+dia+"-"+mes+"-"+anho; // Falta adjuntar la fecha
		File dir = new File(file_path);
		if(!dir.exists())
		dir.mkdirs();
		
		File file = new File(dir, "" +horas+":"+minutos+":"+segundos+".jpg"); // nombre del archivo
		FileOutputStream fOut=null;
		try {
			fOut = new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		imagen.compress(Bitmap.CompressFormat.PNG, 85, fOut);
		try {
			fOut.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			fOut.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		//imagen.setDrawingCacheEnabled(false);
		}

		util.mensaje("Imagen Guardada con Éxito", Foto.this);
	}

}

